<template>
  <div class="taskPackage" :class="className" :style="bcakgroundC">
    <div v-for="(items, ind) in info" :key="ind" class="u_taskPackage">
      <div class="headtitle" :key="ind">{{ items.name }}</div>
      <div class="headtime" :key="ind">有效期{{ items.active_start }}至{{ items.active_end }}</div>
      <div class="taskBox" :key="ind">
        <div class="title">
          <span>完成任务获得{{ usable_name || "可用爱心值" }}</span>
          <div class="toTask" v-if="items.info.can_recovery == 1" @click="makeTask(items)">做任务</div>
          <div class="toTask disable" v-if="items.info.can_recovery == 3 || !items.status">做任务</div>
          <div class="toTask" v-if="items.info.can_recovery == 2" @click="payTask">复活</div>
        </div>
        <div class="tasklineBox">
          <template v-if="items.status">
            <div class="videoList">
              <div class="videoLis" v-for="(item, index) in items.info.video" :key="index">
                <div class="status" v-if="item.status">已完成</div>
                <div class="status_no" v-if="!item.status">未完成</div>
                <div class="line" :class="checkBackground(index, item, items.info.video)">
                  <div class="img">
                    <img src="@/assets/images/taskPackage/video@2x.png" v-if="item.status" alt="" />
                    <img src="@/assets/images/taskPackage/video1@2x.png" v-if="!item.status" alt="" />
                  </div>
                </div>
                <div :class="!item.status ? 'name_default' : ''" class="name">{{ item.name }}</div>
              </div>
            </div>
            <div class="videoList">
              <div class="videoLis" v-for="(item, index) in items.info.share" :key="index">
                <div class="status_pyq" v-if="item.status">已完成</div>
                <div class="status_no" v-if="!item.status">未完成</div>
                <div class="line" :class="checkBackground(index, item, items.info.share)">
                  <div class="imgpyq">
                    <img src="@/assets/images/taskPackage/pyq@2x.png" v-if="item.status" alt="" />
                    <img src="@/assets/images/taskPackage/pyq1@2x.png" v-if="!item.status" alt="" />
                  </div>
                </div>
                <div :class="!item.status ? 'name_default' : ''" class="name_pyq">{{ item.name }}</div>
              </div>
            </div>
            <div class="videoList">
              <div class="videoLis" v-for="(item, index) in items.info.article" :key="index">
                <div class="status_share" v-if="item.status">已完成</div>
                <div class="status_no" v-if="!item.status">未完成</div>
                <div class="line" :class="checkBackground(index, item, items.info.article)">
                  <div class="imgShare">
                    <img src="@/assets/images/taskPackage/fx@2x.png" alt="" v-if="item.status" />
                    <img src="@/assets/images/taskPackage/fx1@2x.png" alt="" v-if="!item.status" />
                  </div>
                </div>
                <div :class="!item.status ? 'name_default' : ''" class="name_share">{{ item.name }}</div>
              </div>
            </div>
          </template>
          <template v-if="!items.status">
            <div class="isNone">暂无任务~</div>
          </template>
        </div>
      </div>
    </div>
    <div class="isnone" v-if="info.length <= 0">暂无任务包</div>
    <div class="moreTap" v-if="isLoadMore && info.length > 0" @click="getMoreData">{{ $i18n.t("加载更多") }}</div>
    <van-popup v-model="showShare" position="bottom" :style="{ background: 'transparent' }" @close="showShareCancel">
      <div class="sharlist">
        <div class="list">
          <div class="lis" v-for="(item, index) in options" :key="index" @click="shareHandle(item)">
            <img :src="item.icon" alt="" />
            <div class="name">{{ item.name }}</div>
          </div>
        </div>
        <div class="cancelBtn" @click="showShareCancel">取消</div>
      </div>
    </van-popup>
  </div>
</template>
<script>
export default {
  props: ["isBottom", "lastIndex", "index", "page_id", "id", "datas", "component_key", "tabcontrol", "isLast"],
  data() {
    return {
      info: [],
      showShare: false,
      options: [
        { name: "视频", icon: "https://mini-app-img-1251768088.cos.ap-guangzhou.myqcloud.com/images%2FtaskPackage%2Fshipin.png", router: "VideoList" },
        { name: "文章", icon: "https://mini-app-img-1251768088.cos.ap-guangzhou.myqcloud.com/images%2FtaskPackage%2Fwenzhang.png", router: "notice" }
      ],
      taskDay: "",
      taskID: "",
      usable_name:'',

      page: 1,
      isLoadMore: true,
      total_page: 0
    };
  },
  computed: {
    className() {
      const name = ["component-wrapper", `component-${this.id}`];
      return name;
    },
    bcakgroundC() {
      if (this.datas.preview_color.isColor == 2) {
        return `
          background-image: url(${this.datas.preview_color.image});
          background-size: contain;
        `;
      } else {
        return `backgroundColor: ${this.datas.preview_color.color}`;
      }
    }
  },
  watch: {
    isBottom: function (newVal, oldVal) {
      if (newVal && this.lastIndex == this.index) {
        this.getMoreData();
      }
    }
  },
  mounted() {
    this.getData();
  },
  methods: {
    shareHandle(item) {
      this.$router.push(this.fun.getUrl(item.router, {}, { taskID: this.taskID, taskDay: this.taskDay }));
    },
    checkBackground(index, item, arr) {
      if (item.status) {
        if (item.last) {
          if (index == arr.length - 1) {
            return "lineSelect_all";
          }
          return "lineSelect";
        } else {
          return "lineSelect_all";
        }
      }
    },
    async getData() {
      let { result, msg, data } = await $http.get("plugin.task-package.frontend.daily.decorate-list", {}, ".");
      console.log(result, msg, data);
      if ((result = 0)) return this.$toast(msg);
      this.isLoadMore = true;
      this.info = data.data;
      this.usable_name = data.usable_name;
      this.total_page = data.last_page;
      if (this.page >= this.total_page) {
        this.isLoadMore = false;
      }
    },
    async getMoreData() {
      this.isLoadMore = false;
      if (this.page >= this.total_page) {
        return;
      }
      this.page = this.page + 1;
      let { result, msg, data } = await $http.get("plugin.task-package.frontend.daily.decorate-list", { page: this.page }, ".");
      console.log(result, msg, data);
      if ((result = 0)) return this.$toast(msg);
      this.isLoadMore = true;
      this.info = this.info.concat(data.data);
      this.total_page = data.last_page;
      if (this.page >= this.total_page) {
        this.isLoadMore = false;
      }
    },
    showShareCancel() {
      this.showShare = false;
      this.taskID = "";
      console.log(this.taskID);
    },
    makeTask(items) {
      this.taskID = items.id;
      this.showShare = true;
      let currentDate = new Date();
      let currentYear = currentDate.getFullYear();
      let currentMonth = currentDate.getMonth() + 1;
      let currentDay = currentDate.getDate();
      this.taskDay = `${currentYear}-${currentMonth}-${currentDay}`;
    }
  }
};
</script>
<style lang="scss" scoped>
.taskPackage {
  padding: 0.63rem;
}

.isnone {
  width: 100%;
  line-height: 40px;
  text-align: center;
  clear: both;
  color: #999;
}

.moreTap {
  width: 100%;
  height: 40px;
  line-height: 40px;
  text-align: center;
  clear: both;
  color: #999;
}

.sharlist {
  height: 100%;
  display: flex;
  flex-direction: column;
  background: #7b7c7d;
  border-radius: 1rem 1rem 0 0;

  .cancelBtn {
    height: 2.81rem;
    background: #fff;
    text-align: center;
    line-height: 2.81rem;
  }

  .list {
    display: flex;
    justify-content: space-around;
    margin-bottom: 0.28rem;
    background: #fff;
    border-radius: 1rem 1rem 0 0;

    .lis {
      padding: 1.71rem 0 1.66rem 0;
      text-align: center;

      img {
        width: 2.72rem;
        height: 2.72rem;
        border-radius: 100%;
      }

      .name {
        font-size: 0.81rem;
        color: #333;
        font-weight: 400;
      }
    }
  }
}

.headtitle {
  background: #fff;
  font-size: 0.94rem;
  font-weight: 400;
  color: #242424;
  padding-bottom: 0.56rem;
  text-align: center;
  padding-top: 0.5rem;
  border-radius: 0.31rem 0.31rem 0 0;
}

.headtime {
  background: #fff;
  font-size: 0.75rem;
  font-weight: 400;
  color: #000;
  text-align: center;
}

.taskBox {
  padding: 1rem 0;
  background: #fff;
  border-radius: 0 0 0.31rem 0.31rem;
  font-size: 0.88rem;
  margin-bottom: 1rem;

  .tasklineBox {
    margin-top: 1.19rem;

    .isNone {
      font-size: 0.75rem;
      color: #9a9a9a;
      text-align: center;
      margin-top: 2.13rem;
    }

    .videoList::-webkit-scrollbar {
      width: 0;
    }

    .videoList:last-child {
      margin-bottom: 0;
    }

    .videoList {
      display: flex;
      overflow-x: scroll;
      // margin-bottom: 0.84rem;
      .videoLis:last-child {
        padding-right: 0.87rem;
      }

      .videoLis:first-child {
        padding-left: 0.87rem;
      }

      .videoLis:first-child .line {
        border-radius: 0.19rem 0 0 0.19rem;
      }

      .videoLis:last-child .line {
        border-radius: 0 0.19rem 0.19rem 0;
      }

      .videoLis {
        position: relative;
        flex-shrink: 0;
        width: 20%;
        display: flex;
        flex-direction: column;
        align-items: center;

        .status {
          font-size: 0.63rem;
          font-weight: 400;
          color: #f55e55;
          // line-height: 0.63rem;
        }

        .status_pyq {
          font-size: 0.63rem;
          font-weight: 400;
          color: #07c160;
          // line-height: 0.63rem;
        }

        .status_share {
          font-size: 0.63rem;
          font-weight: 400;
          color: #29abe2;
          // line-height: 0.63rem;
        }

        .status_no {
          font-size: 0.63rem;
          font-weight: 400;
          color: #9a9a9a;
          // line-height: 0.63rem;
        }

        .lineSelect {
          background: linear-gradient(to right, #f55e55 0%, #f55e55 50%, #e6e6e6 51%, #e6e6e6 100%) !important;
        }

        .lineSelect_all {
          background: #f55e55 !important;
        }

        .line {
          width: 100%;
          height: 0.38rem;
          background: #e6e6e6;
          margin: 0.91rem 0 0.97rem 0;
          display: flex;
          align-items: center;
          justify-content: center;

          .imgpyq {
            width: 1.25rem;
            height: 1.25rem;
            border-radius: 100%;

            img {
              width: 1.25rem;
              height: 1.25rem;
              border-radius: 100%;
            }
          }

          .imgShare {
            width: 1.31rem;
            height: 1.28rem;
            background: #fff;

            img {
              width: 1.31rem;
              height: 1.28rem;
            }
          }

          .img {
            width: 1.41rem;
            height: 1.06rem;

            img {
              width: 1.41rem;
              height: 1.06rem;
            }
          }
        }

        .name {
          font-size: 0.69rem;
          font-weight: 400;
          color: #f55e55;
        }

        .name_pyq {
          font-size: 0.69rem;
          font-weight: 400;
          color: #07c160;
        }

        .name_share {
          font-size: 0.69rem;
          font-weight: 400;
          color: #29abe2;
        }

        .name_default {
          color: #000;
        }
      }
    }
  }

  .title {
    display: flex;
    justify-content: space-between;
    padding: 0 0.69rem 0 0.72rem;
    align-items: center;

    span {
      font-size: 0.88rem;
      color: #242424;
    }

    .toTask {
      line-height: 0.66rem;
      padding: 0.41rem 0.78rem;
      background: #ff8924;
      border-radius: 0.75rem;
      font-size: 0.69rem;
      color: #fff;
    }

    .disable {
      background: #bcbcbc;
    }
  }
}
</style>